const express = require("express");
const router = express.Router();
const Response = require("../utils/Response.js");
const fs = require("fs");

// 引入mysql连接池
const pool = require("../utils/db.js");

// 新增分享评论
router.post("/add_share", (req, res, next) => {
  let obj = req.body;
  pool.query("insert into comment_share set uid=?,content=?,time=?,sid=?", [obj.uid,obj.content,obj.time,obj.sid], (err, result) => {
    if (err) {
      return next(err);
    }
    console.log(result);
    if (result.affectedRows === 0) {
      res.send({ code: 201, msg: "新增失败" });
    } else {
      res.send({ code: 200, msg: "新增成功" });
    }
  });
});

//   通过share_id查询分享评论
router.get("/share", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  pool.query("select comment_share.*,user.headpic,user.uname from comment_share,user where sid=? and user.uid=comment_share.uid", [obj.sid], (err, result) => {
    if (err) {
      return next(err);
    }
    console.log(result);
    //结果是对象，如果对象下的属性length为0说明修改失败
    if (result.length === 0) {
      res.send({ code: 201, msg: "查询失败" });
    } else {
      res.send({ code: 200, msg: "查询成功", data: result });
    }
  });
});

//   查询所有分享评论
router.get("/all_share", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  pool.query("select comment_share.*,user.headpic,user.uname from comment_share,user where user.uid=comment_share.uid order by id", [], (err, result) => {
    if (err) {
      return next(err);
    }
    console.log(result);
    //结果是对象，如果对象下的属性length为0说明修改失败
    if (result.length === 0) {
      res.send({ code: 201, msg: "查询失败" });
    } else {
      res.send({ code: 200, msg: "查询成功", data: result });
    }
  });
});

// 删除分享评论的内容
router.post("/delete_share", (req, res, next) => {
  let obj = req.body;
  console.log(obj);
  pool.query("delete from comment_share where id=?", [obj.id], (err, result) => {
    if (err) {
      return next(err);
    }
    if (result.affectedRows === 0) {
      res.send({ code: 201, msg: "删除失败" });
    } else {
      res.send({ code: 200, msg: "删除成功" });
    }
  });
});


// 新增课程评论
router.post("/add_course", (req, res, next) => {
  let obj = req.body;
  pool.query("insert into comment_course set uid=?,content=?,time=?,pid=?", [obj.uid,obj.content,obj.time,obj.pid], (err, result) => {
    if (err) {
      return next(err);
    }
    console.log(result);
    if (result.affectedRows === 0) {
      res.send({ code: 201, msg: "新增失败" });
    } else {
      res.send({ code: 200, msg: "新增成功" });
    }
  });
});

//   通过cou_id查询课程评论
router.get("/course", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  pool.query("select comment_course.*,user.headpic,user.uname from comment_course,user where pid=? and user.uid=comment_course.uid order by id", [obj.pid], (err, result) => {
    if (err) {
      return next(err);
    }
    console.log(result);
    //结果是对象，如果对象下的属性length为0说明修改失败
    if (result.length === 0) {
      res.send({ code: 201, msg: "查询失败" });
    } else {
      res.send({ code: 200, msg: "查询成功", data: result });
    }
  });
});

//   查询所有课程评论
router.get("/all_course", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  pool.query("select comment_course.*,user.headpic,user.uname from comment_course,user where user.uid=comment_course.uid", [], (err, result) => {
    if (err) {
      return next(err);
    }
    console.log(result);
    //结果是对象，如果对象下的属性length为0说明修改失败
    if (result.length === 0) {
      res.send({ code: 201, msg: "查询失败" });
    } else {
      res.send({ code: 200, msg: "查询成功", data: result });
    }
  });
});

// 删除课程评论的内容
router.post("/delete_course", (req, res, next) => {
  let obj = req.body;
  console.log(obj);
  pool.query("delete from comment_course where id=?", [obj.id], (err, result) => {
    if (err) {
      return next(err);
    }
    if (result.affectedRows === 0) {
      res.send({ code: 201, msg: "删除失败" });
    } else {
      res.send({ code: 200, msg: "删除成功" });
    }
  });
});
module.exports = router;
